# coding:utf-8
import os
import re
import time
import pickle


def convert_str_to_time(str_time):
    '''
    把字符串的时间转换为格林威治时间
    :param str_time:   2016-09-22 11:01:01 
    :return: 1474513261.0
    '''
    l = re.split(',| |-|:', str_time)
    l = [int(item) for item in l]
    l += [0, 0, 0]
    return time.mktime(tuple(l))


def convert_time_to_str(t):
    '''
    把格林位置时间转换为str的时间
    :param t: 1474513261
    :return: 2016-09-22 11:01:01
    '''
    local_t = time.localtime(t)
    return time.strftime('%Y-%m-%d %H:%M:%S', local_t)


def save_object(filename, obj):
    with open(filename, 'wb') as f:
        pickle.dump(obj, f)


def load_object(filename):
    with open(filename, 'r') as f:
        return pickle.load(f)


# 除了中文、字母、数字、普通标点，之外的所有字符
regexStr0 = r'[^\u4e00-\u9f5a\w<>：.。!！]'
# 除了中文、字母、数字之外的所有字符
regexStr1 = r'[^\u4e00-\u9f5a\w<>]'
regexStr2 = r'<style>.*?</style>'
regexStr3 = r'\s+'
regexStr4 = r'<[^>]+>'
regexStr5 = r'<\s*/\s*p\s*>'
# 表情
regexStr6 = r'&[a-z]+;'
regex5 = re.compile(regexStr5, re.S)  # 把</p>变成一个句号
regexStr = '|'.join((regexStr6, regexStr1, regexStr2, regexStr3, regexStr4))
regex2 = re.compile(regexStr, re.S)  # 去掉style标签的内容


# re.sub(ur\s+\.\!\/_,$%^*(+\"\']+|[+——！，。？、~@#￥%……&*（）]+", "", content)
def clean_data(content):
    '''
    去掉特殊符号，去掉html特殊标签
    :param content: 
    :return: 
    '''
    content = re.sub(pattern=regex2, string=content, repl=' ')
    content = content.strip()
    return content


regex3 = re.compile('|'.join((regexStr0, regexStr2, regexStr3, regexStr4)), re.S)


def clean_data_except_mark(content):
    '''
    输入内容是unicode，作用是去掉特殊符号，去掉html特殊标签
    :param content: 
    :return: 
    '''
    content = re.sub(pattern=regex5, string=content, repl='.')
    content = re.sub(pattern=regex3, string=content, repl='')
    content = content.strip()
    # content = re.sub(ur"[【】：，,.。]+!:", " ", content) #去掉特定符号
    return content


def deal_title_in_content(title, content):
    '''
    如果content中完全包含title，则直接从content中去掉title
    :param title: str 
    :param content: str
    :return: str
    '''
    if re.match(title, content):
        return re.sub(pattern=title, repl='', string=content, count=1)
    return content


def current_dir_path(file):
    return os.path.split(os.path.realpath(file))[0] + '/'


def get_script_name(absolute_path):
    return os.path.basename(absolute_path).split('.')[0]


if __name__ == '__main__':
    content = r'&lt;p&gt;看多纳和小伙伴的故事，帮宝宝养成好性格！&lt;/p&gt;'
    print(clean_data(content))
    print(clean_data_except_mark(content))
